import { setOpinion } from '../../../api/myApi';
import { baseUrl } from "../../../config";
import * as util from "../../../utils/util";


// pages/suggestion.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    smallNum: 0,
    imgNum: 0,
    contentLength: 0,
    imgReduce: {
      one: '',
      two: '',
      three: '',
      four: ''
    },
    opinData: {
      content: '',
      opinionImg1: '',
      opinionImg2: '',
      opinionImg3: '',
      opinionImg4: ''
    }
  },
  inputLength(e) {
    let content = e.detail.value
    this.setData({
      'opinData.content': content,
      contentLength: content.length
    })
  },
  uploadImg() {
    var that = this
    wx.chooseImage({
      count: 4 - this.data.imgNum,
      sourceType: ['album', 'camera'],
      success(res) {
        wx.showLoading({
          title: '正在上传',
        })
        // if (that.data.imgNum == 0) {
        //   that.setData({
        //     'imgReduce.one':imgNum
        //       })
        // }
        var tempFilePaths = res.tempFilePaths
        // if (res.tempFiles[0].size > 4096) {
        //   wx.hideLoading();
        //   wx.showToast({
        //     icon: 'none',
        //     title: '单张张片超过4M,请重新添加'
        //   });
        //   return
        // } else {
        for (var i = 0; i < tempFilePaths.length; i++) {
          that.toSmImg(tempFilePaths[i], i)
          wx.uploadFile({
            url: baseUrl + '/applets/oss/uploadFile', //仅为示例，非真实的接口地址
            filePath: tempFilePaths[i],
            name: 'file',
            formData: {},
            success(res) {
              let data = JSON.parse(res.data)
              that.toBgImg(data.body.url)
              let imgNum = that.data.imgNum + 1
              wx.hideLoading();
              that.setData({
                imgNum: imgNum
              })
            }
          })
        }

      }
    })
  },
  submitOpin() {
    if (this.data.contentLength <= 0) {
      util.toast('请输入需要反馈的内容');
    } else {
      setOpinion(this.data.opinData).then(res => {
        wx.showToast({
          title: '感谢您的反馈',
          image: '/images_keep/smile.png',
          duration: 2000,
          icon: 'none'
        });
        setTimeout(() => {
          wx.reLaunch({
            url: '/pages/home/home?presentPage=userInfo'
          })
        }, 2500)

      }, err => {
        util.toast('网络繁忙，请稍后再试');
      });
    }

  },
  toBgImg(imgUrl) {
    let num = this.data.imgNum
    switch (num) {
      case 0:
        this.data.opinData.opinionImg1 = imgUrl
        break;
      case 1:
        this.data.opinData.opinionImg2 = imgUrl
        break;
      case 2:
        this.data.opinData.opinionImg3 = imgUrl
        break;
      case 3:
        this.data.opinData.opinionImg4 = imgUrl
        break;
    }
  },
  toSmImg(imgUrl) {
    let smallNum = this.data.smallNum;
    switch (smallNum) {
      case 0:
        this.setData({
          'imgReduce.one': imgUrl,
          'smallNum': ++smallNum
        })
        break;
      case 1:
        this.setData({
          'imgReduce.two': imgUrl,
          'smallNum': ++smallNum
        })
        break;
      case 2:
        this.setData({
          'imgReduce.three': imgUrl,
          'smallNum': ++smallNum
        })
        break;
      case 3:
        this.setData({
          'imgReduce.four': imgUrl,
          'smallNum': ++smallNum,
        })
        break;
    }
  },


  remove: function (e) {
    let num = e.currentTarget.dataset.num;
    let smallNum = this.data.smallNum;
    let imgNum = this.data.imgNum;
    if (num == "one") {
      this.setData({
        smallNum: --smallNum,
        imgNum: --imgNum,
        'imgReduce.one': this.data.imgReduce.two,
        'imgReduce.two': this.data.imgReduce.three,
        'imgReduce.three': this.data.imgReduce.four,
        'imgReduce.four': '',
        'opinData.opinionImg1': this.data.opinData.opinionImg2,
        'opinData.opinionImg2': this.data.opinData.opinionImg3,
        'opinData.opinionImg3': this.data.opinData.opinionImg4,
        'opinData.opinionImg4': '',
      })
    } else if (num == "two") {
      this.setData({
        smallNum: --smallNum,
        imgNum: --imgNum,
        'imgReduce.two': this.data.imgReduce.three,
        'imgReduce.three': this.data.imgReduce.four,
        'imgReduce.four': '',
        'opinData.opinionImg2': this.data.opinData.opinionImg3,
        'opinData.opinionImg3': this.data.opinData.opinionImg4,
        'opinData.opinionImg4': '',
      })
    } else if (num == "three") {
      this.setData({
        smallNum: --smallNum,
        imgNum: --imgNum,
        'imgReduce.three': this.data.imgReduce.four,
        'imgReduce.four': '',
        'opinData.opinionImg3': this.data.opinData.opinionImg4,
        'opinData.opinionImg4': '',
      })
    } else if (num == "four") {
      this.setData({
        smallNum: --smallNum,
        imgNum: --imgNum,
        'imgReduce.four': '',
        'opinData.opinionImg4': '',
      })
    }

  },

  showImage: function (e) {
    var url = e.currentTarget.dataset.image;
    wx.navigateTo({
      url: '/pages/common/perviewPhoto/perviewPhoto?origin=feedback&url=' + url
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})